<template>
  <view class="login-container">
    <!-- 背景装饰 -->
    <view class="bg-decoration">
      <view class="circle circle-1"></view>
      <view class="circle circle-2"></view>
      <view class="circle circle-3"></view>
    </view>
    
    <!-- 登录内容 -->
    <view class="login-content">
      <!-- Logo和标题 -->
      <view class="header">
        <view class="logo">
          <uni-icons type="water" size="60" color="#ffffff"></uni-icons>
        </view>
        <text class="title">水质监测系统</text>
        <text class="subtitle">Water Quality Monitoring System</text>
      </view>
      
      <!-- 登录表单 -->
      <view class="login-form">
        <view class="form-item">
          <view class="input-wrapper">
            <uni-icons type="person" size="20" color="#2E7D32"></uni-icons>
            <input 
              v-model="loginForm.username" 
              placeholder="请输入用户名" 
              class="input-field"
              placeholder-style="color: #999"
            />
          </view>
        </view>
        
        <view class="form-item">
          <view class="input-wrapper">
            <uni-icons type="locked" size="20" color="#2E7D32"></uni-icons>
            <input 
              v-model="loginForm.password" 
              placeholder="请输入密码" 
              :password="!showPassword"
              class="input-field"
              placeholder-style="color: #999"
            />
            <uni-icons 
              :type="showPassword ? 'eye-slash' : 'eye'" 
              size="20" 
              color="#999" 
              @click="togglePassword"
            ></uni-icons>
          </view>
        </view>
        
        <view class="form-options">
          <view class="remember-me">
            <checkbox :checked="rememberMe" @change="onRememberChange" color="#2E7D32" />
            <text class="option-text">记住密码</text>
          </view>
          <text class="forgot-password" @click="forgotPassword">忘记密码？</text>
        </view>
        
        <button class="login-btn" @click="handleLogin" :disabled="loading">
          <uni-icons v-if="loading" type="spinner-cycle" size="20" color="#ffffff"></uni-icons>
          <text>{{ loading ? '登录中...' : '登录' }}</text>
        </button>
        
        <view class="register-link">
          <text>还没有账号？</text>
          <text class="link" @click="goToRegister">立即注册</text>
        </view>
      </view>
    </view>
    
    
    <!-- 底部信息 -->
    <view class="footer">
      <text class="footer-text">© 2024 水质监测系统 版权所有</text>
      <text class="footer-text">技术支持：智能监测团队</text>
    </view>
  </view>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useUserStore } from '@/stores/user.js'

// 使用用户状态管理
const userStore = useUserStore()

// 响应式数据
const loading = ref(false)
const rememberMe = ref(false)
const showPassword = ref(false)

// 登录表单
const loginForm = reactive({
  username: '',
  password: ''
})

// 组件挂载时检查登录状态
onMounted(() => {
  if (userStore.checkLoginStatus()) {
    // 已登录，跳转到首页
    uni.switchTab({
      url: '/pages/index/index'
    })
  }
})

// 切换密码显示
const togglePassword = () => {
  showPassword.value = !showPassword.value
}


// 记住密码切换
const onRememberChange = (e) => {
  rememberMe.value = e.detail.value
}

// 处理登录
const handleLogin = async () => {
  if (!loginForm.username.trim()) {
    uni.showToast({ title: '请输入用户名', icon: 'none' })
    return
  }
  
  if (!loginForm.password.trim()) {
    uni.showToast({ title: '请输入密码', icon: 'none' })
    return
  }
  
  loading.value = true
  
  try {
    const result = await userStore.login(loginForm)
    
    if (result.success) {
      uni.showToast({ title: result.message, icon: 'success' })
      
      // 跳转到首页
      setTimeout(() => {
        uni.switchTab({
          url: '/pages/index/index'
        })
      }, 1500)
    } else {
      uni.showToast({ title: result.message, icon: 'none' })
    }
    
  } catch (error) {
    console.error('登录错误:', error)
    uni.showToast({ title: '登录失败，请稍后重试', icon: 'error' })
  } finally {
    loading.value = false
  }
}

// 跳转到注册页面
const goToRegister = () => {
  uni.navigateTo({
    url: '/pages/register/register'
  })
}


// 忘记密码
const forgotPassword = () => {
  uni.showModal({
    title: '忘记密码',
    content: '请联系系统管理员重置密码',
    showCancel: false
  })
}
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  background: linear-gradient(135deg, #f8fffe 0%, #e8f5f0 100%);
  position: relative;
  overflow: hidden;
}

.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(46, 125, 50, 0.1);
  
  &.circle-1 {
    width: 200rpx;
    height: 200rpx;
    top: -100rpx;
    right: -100rpx;
  }
  
  &.circle-2 {
    width: 150rpx;
    height: 150rpx;
    bottom: 200rpx;
    left: -75rpx;
  }
  
  &.circle-3 {
    width: 100rpx;
    height: 100rpx;
    top: 300rpx;
    right: 50rpx;
  }
}

.login-content {
  position: relative;
  z-index: 1;
  padding: 100rpx 60rpx 60rpx;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.header {
  text-align: center;
  margin-bottom: 80rpx;
}

.logo {
  width: 120rpx;
  height: 120rpx;
  background: linear-gradient(135deg, #2E7D32, #4CAF50);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 30rpx;
  box-shadow: 0 8rpx 32rpx rgba(46, 125, 50, 0.3);
}

.title {
  font-size: 48rpx;
  font-weight: bold;
  color: #2E7D32;
  margin-bottom: 10rpx;
  display: block;
}

.subtitle {
  font-size: 24rpx;
  color: #4CAF50;
}

.login-form {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20rpx;
  padding: 60rpx 40rpx;
  margin-bottom: 40rpx;
  backdrop-filter: blur(10px);
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.1);
}

.form-item {
  margin-bottom: 40rpx;
}

.input-wrapper {
  display: flex;
  align-items: center;
  background: #f8f9fa;
  border-radius: 12rpx;
  padding: 0 20rpx;
  height: 80rpx;
}

.input-field {
  flex: 1;
  margin-left: 20rpx;
  font-size: 28rpx;
  color: #333;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40rpx;
}

.remember-me {
  display: flex;
  align-items: center;
}

.option-text {
  font-size: 24rpx;
  color: #666;
  margin-left: 10rpx;
}

.forgot-password {
  font-size: 24rpx;
  color: #2E7D32;
}

.login-btn {
  width: 100%;
  height: 80rpx;
  background: linear-gradient(135deg, #2E7D32, #4CAF50);
  border-radius: 12rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32rpx;
  font-weight: bold;
  color: #ffffff;
  border: none;
  box-shadow: 0 4rpx 20rpx rgba(46, 125, 50, 0.3);
  margin-bottom: 30rpx;
  
  &:disabled {
    opacity: 0.6;
  }
}

.register-link {
  text-align: center;
  font-size: 24rpx;
  color: #666;
  
  .link {
    color: #2E7D32;
    margin-left: 10rpx;
  }
}


.footer {
  text-align: center;
  margin-top: auto;
}

.footer-text {
  display: block;
  font-size: 20rpx;
  color: rgba(46, 125, 50, 0.6);
  margin-bottom: 10rpx;
}
</style>